/* 
 * File:   Database.h
 * Author: alrojas
 *
 * Created on 16 de junio de 2014, 11:13
 */

#ifndef DATABASE_H
#define	DATABASE_H

#include <pqxx/pqxx>
#include <iostream>

using namespace std;
using namespace pqxx;

namespace Database{
    
    /**
     * Ejecuta una consulta en la base de datos
     * @param query consulta a ejecutar
     */
    inline void Execute(string query){
        try{
            connection conn("dbname=postgres user=postgres password=uno23456 hostaddr=127.0.0.1 port=5432");
            work w(conn);

            if(conn.is_open()){
                w.exec(query);
                w.commit();
            }
        }
        catch(const exception &e){
            //cerr << "Exception: " << e.what() << endl;
        }
    }
    
    /**
     * Selecciona uno ó mas registros de la base de datos
     * @param query consulta a ejecutar
     */
    inline result Execute2(string query){
        
        result records;
        try{
            connection conn("dbname=postgres user=postgres password=uno23456 hostaddr=127.0.0.1 port=5432");
            work w(conn);

            if(conn.is_open()){
                records = w.exec(query);
                                
                int count = uint(records.size());
                if (count > 0){
                    return records;
                }
                else{
                }
            }
        }
        catch(const exception &e){
            //cerr << "Exception: " << e.what() << endl;
        }
        
        return records;
    }
}

#endif	/* DATABASE_H */

